﻿
Public Class rc_pac_client
    Private ReadOnly talk As tcp_pac_talk = Nothing

    Public Function invoke(ByVal command As String,
                           ByVal arg As envelope,
                           ByVal found As pointer(Of Boolean),
                           ByVal result As pointer(Of Boolean),
                           ByVal rtn As pointer(Of envelope),
                           ByRef ec As eventComb) As Boolean
        Dim iec As eventComb = Nothing
        Dim res As pointer(Of response) = Nothing
        res = New pointer(Of response)()
        If invoke(New request(command, arg), res, iec) Then
            ec = New eventComb(Function() As Boolean
                                   Return waitfor(iec) AndAlso goto_next()
                               End Function,
                               Function() As Boolean
                                   If iec.end_result() AndAlso Not res.get() Is Nothing Then
                                       Return eva(found, (+res).found) AndAlso
                                              eva(result, (+res).result) AndAlso
                                              eva(rtn, (+res).argument) AndAlso
                                              goto_end()
                                   Else
                                       Return False
                                   End If
                               End Function)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function invoke(ByVal req As request,
                           ByVal res As pointer(Of response),
                           ByRef ec As eventComb) As Boolean
        Dim s As envelope = Nothing
        If pack_request(req, s) Then
            Dim o As pointer(Of envelope) = Nothing
            o = New pointer(Of envelope)()
            Dim tec As eventComb = Nothing
            ec = New eventComb(Function() As Boolean
                                   tec = talk.talk(s, o)
                                   Return waitfor(tec) AndAlso goto_next()
                               End Function,
                               Function() As Boolean
                                   If tec.end_result() Then
                                       Return unpack_response(+o, res) AndAlso
                                              goto_end()
                                   Else
                                       Return False
                                   End If
                               End Function)
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub New(ByVal host As String, ByVal port As Int32)
        talk = New tcp_pac_talk(host, port)
    End Sub
End Class

Public Class rc_pac_client_pool
    Inherits pool3(Of rc_pac_client, _true)

    Private ReadOnly host As String = Nothing
    Private ReadOnly port As Int32 = 0

    Public Sub New(ByVal host As String, ByVal port As Int32)
        MyBase.New()
        copy(Me.host, host)
        copy(Me.port, port)
    End Sub

    Protected Overrides Function create() As rc_pac_client
        Return New rc_pac_client(host, port)
    End Function

    Public Function invoke(ByVal req As request,
                           ByVal res As pointer(Of response),
                           ByRef ec As eventComb) As Boolean
        Dim c As rc_pac_client = Nothing
        c = [get]()
        If c Is Nothing Then
            Return False
        Else
            Dim cec As eventComb = Nothing
            If c.invoke(req, res, cec) Then
                ec = New eventComb(Function() As Boolean
                                       Return waitfor(cec) AndAlso goto_next()
                                   End Function,
                                   Function() As Boolean
                                       release(c)
                                       Return cec.end_result() AndAlso goto_end()
                                   End Function)
                Return True
            Else
                Return False
            End If
        End If
    End Function
End Class
